package algorithm.poj.p1000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLDecoder;

/**
 * 分析：
 * 实现：
 * 经验：
 * 教训：
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P1426WA {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {

		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P1426WA.class.getResource("P1426.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		StringBuffer sb = new StringBuffer();
		String line = stdin.readLine();
		while (line != null && !"0".equals(line)) {
			int n = Integer.valueOf(line);
			BigInteger t = f(n);
			sb.append(t.toString()).append("\n");
			line = stdin.readLine();
		}
		System.out.println(sb);
//		for (int n = 1; n <= 200; n ++) {
//			BigInteger t = f(n);
//			System.out.println(n + "\t"+ t);
//		}
	}

	/**
	 * 返回形如A*(10^m-1)/(10^k-1)的位数不超过100的n的倍数。 
	 * 其中A各位都是0，1且不超过k位数，m是k的倍数。
	 * @param n
	 * @return
	 */
	private static BigInteger f(int n) {
		
		int pow2 = 0;
		while (n%2 == 0) {
			pow2 ++;
			n /= 2;
		}
		int pow5 = 0;
		while (n%5 == 0) {
			pow5 ++;
			n /= 5;
		}
		int p = Math.max(pow2, pow5);
		int d = d(n);
		BigInteger N = BigInteger.valueOf(n);

		int t = 1;
		while (t < 200/d+1) {
			int w = d*t;
			BigInteger x = BigInteger.TEN.pow(w).subtract(BigInteger.ONE);
			for (int k = w; k > 0; k --) {
				if (w%k == 0) {
					for (int a = 1; a < Math.pow(2, Math.min(10, k)); a ++) {
						BigInteger A = new BigInteger(Integer.toBinaryString(a)); 
						BigInteger y = BigInteger.TEN.pow(k).subtract(BigInteger.ONE);
						BigInteger z = x.divide(y).multiply(A);
						if (z.mod(N).compareTo(BigInteger.ZERO) == 0) { 
							z = z.multiply(BigInteger.TEN.pow(p));
							if (z.toString().length() < 100) {
								return z;
							}
						}
					}
				}
			}
			t ++;
		}
		return BigInteger.valueOf(-1);
	}
	
	private static int[] ps = new int[] {
		2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 
		53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 
		109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 
		173, 179, 181, 191, 193, 197, 199 
	};
	
	private static int phi(int n) {
		
		int m = n;
		for (int i = 0; i < ps.length; i ++) {
			if (n%ps[i] == 0) m = m/ps[i]*(ps[i]-1);
		}
		return m;
	}
	
	private static int d(int n) {
		
		int m = phi(n);
		BigInteger N = BigInteger.valueOf(n);
		for (int i = 1; i <= m; i ++) {
			if (m%i == 0) {
				BigInteger w = BigInteger.TEN.pow(i).subtract(BigInteger.ONE);
				if (w.mod(N).compareTo(BigInteger.ZERO) == 0) {
					return i;
				}
			}
		}
		return -1;
	}
}